package com.hengxinguotong.zhihuichengjian.widget.view;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.utils.Utils;

import java.util.List;

/**
 * Created by liux on 16/3/15.
 */
public class KeyboardPop extends PopupWindow implements View.OnClickListener{

    private final LinearLayout popRoot;
    private Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0,btDot,btBack, btClear;
    private TextView cancel_bt;
    private View blackView;
    private EditText inputEt;
    private StringBuffer sb;

    public KeyboardPop(Activity act,int layoutID, EditText inputEt) {
        super(act);
        this.inputEt = inputEt;
        popRoot = (LinearLayout) LayoutInflater.from(act).inflate(layoutID, null);
//        setAnimationStyle(R.style.dialogSelectAnimat);
        setWidth(Utils.getWindowWidth(act));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setContentView(popRoot);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setOutsideTouchable(true);
        sb = new StringBuffer();
        initView();
    }

    private void initView() {
        bt0 = (Button) popRoot.findViewById(R.id.bt_0);
        bt0.setOnClickListener(this);
        bt1 = (Button) popRoot.findViewById(R.id.bt_1);
        bt1.setOnClickListener(this);
        bt2 = (Button) popRoot.findViewById(R.id.bt_2);
        bt2.setOnClickListener(this);
        bt3 = (Button) popRoot.findViewById(R.id.bt_3);
        bt3.setOnClickListener(this);
        bt4 = (Button) popRoot.findViewById(R.id.bt_4);
        bt4.setOnClickListener(this);
        bt5 = (Button) popRoot.findViewById(R.id.bt_5);
        bt5.setOnClickListener(this);
        bt6 = (Button) popRoot.findViewById(R.id.bt_6);
        bt6.setOnClickListener(this);
        bt7 = (Button) popRoot.findViewById(R.id.bt_7);
        bt7.setOnClickListener(this);
        bt8 = (Button) popRoot.findViewById(R.id.bt_8);
        bt8.setOnClickListener(this);
        bt9 = (Button) popRoot.findViewById(R.id.bt_9);
        bt9.setOnClickListener(this);
        btDot = (Button) popRoot.findViewById(R.id.bt_dot);
        btDot.setOnClickListener(this);
        btBack = (Button) popRoot.findViewById(R.id.bt_back);
        btBack.setOnClickListener(this);
        btClear = (Button) popRoot.findViewById(R.id.bt_clear);
        btClear.setOnClickListener(this);
        blackView = popRoot.findViewById(R.id.dialog_imgdir_name_view);
        blackView.setOnClickListener(this);
        cancel_bt = (TextView) popRoot.findViewById(R.id.cancel_bt);
        cancel_bt.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.cancel_bt:
            case R.id.dialog_imgdir_name_view:
                dismiss();
                break;
            case R.id.bt_back:
                sb.deleteCharAt(sb.length() - 1);
                inputEt.setText(sb.toString());
                inputEt.setSelection(sb.length());
                break;
            case R.id.bt_clear:
                sb = new StringBuffer();
                inputEt.setText(sb.toString());
                inputEt.setSelection(sb.length());
                break;
            case R.id.bt_0:
            case R.id.bt_1:
            case R.id.bt_2:
            case R.id.bt_3:
            case R.id.bt_4:
            case R.id.bt_5:
            case R.id.bt_6:
            case R.id.bt_7:
            case R.id.bt_8:
            case R.id.bt_9:
            case R.id.bt_dot:
                sb.append(((Button)view).getText().toString());
                inputEt.setText(sb.toString());
                inputEt.setSelection(sb.length());
                break;
        }
    }
}
